// leaderPerson/pages/leaderIncome/leaderIncome.js
var common = require("../../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        page: 1,
        format: 1,
        type: 2,
        list: [],
        bro: 0
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            domain: app.globalData.domain.slice(0, -10),
            clerk: wx.getStorageSync("clerk"),
            format: options.tab || 1
        });
        if (this.data.format == 1) {
            this.leaderBrokerage();
        } else {
            this.getinfo();
        }
        this.getBase();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        this.setData({
            page: 1
        });
        if (this.data.format == 1) {
            this.leaderBrokerage();
        } else {
            this.getinfo();
        }
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        this.leaderBrokerage();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //商品，当面付切换
    chooseOrder(e) {
        console.log(e);
        this.setData({
            format: e.currentTarget.dataset.i,
            page: 1,
            list: []
        });
        this.leaderBrokerage();
    },
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                that.setData({
                    color_main: res.extra.color_main,
                    time_choose: res.extra.time_choose,
                    apptitle: res.extra.title,
                    leader_share: res.extra.leader_share,
                    user_share: res.extra.user_share,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    if_leadersales_show: res.extra.if_leadersales_show,
                    leader_detail_edit: res.extra.leader_detail_edit,
                    phone: res.extra.phone,
                    sleep_on: res.extra.sleep_on
                });
            }
        });
    },
    chooseFormat: function(e) {
        this.setData({
            format: e.currentTarget.dataset.format,
            page: 1
        });
        if (this.data.format == 1) {
            this.leaderBrokerage();
        } else {
            this.getinfo();
        }
    },
    getinfo: function(page) {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        common.request({
            endpoint: "finance/brokerage_apply",
            method: "post",
            data: {
                openid: openid
            },
            header: {
                "content-type": "application/json"
            },
            success: function(res) {
                console.log(res);
                that.setData({
                    bill2: res.extra.bill
                });
            }
        });
    },
    chooseType: function(e) {
        this.setData({
            type: e.currentTarget.dataset.type,
            page: 1
        });
        this.leaderBrokerage();
    },
    leaderBrokerage: function(e) {
        var _this = this;
        let {page: page, format: format, type: type} = _this.data;
        common.request({
            endpoint: "My/leader_under/leader-brokerage",
            method: "post",
            data: {
                page: page,
                format: format,
                type: type
            },
            success: res => {
                var list = res.extra.detail;
                list = page == 1 ? list : _this.data.list.concat(list);
                this.setData({
                    bro: res.extra.bro || 0,
                    day: res.extra.day,
                    brokerage_re: res.extra.brokerage_re,
                    face_pay_brokerage: res.extra.face_pay_brokerage,
                    list: list,
                    page: page + 1
                });
            },
            fail(res) {
                wx.showModal({
                    title: "提示",
                    content: res.message,
                    showCancel: false
                });
            }
        });
    }
});